package com.hpxx.ylzswl.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.hpxx.ylzswl.activity.MessageActivity;
import com.hpxx.ylzswl.event.OrderIdEvent;
import com.hpxx.ylzswl.event.OrderListEvent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import de.greenrobot.event.EventBus;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.d("lzx", "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
        // TODO Auto-generated method stub
        //判断borad接受的类型
        if(intent.getAction().equals(JPushInterface.ACTION_MESSAGE_RECEIVED)){
            //获得message的内容

            String title = bundle.getString(JPushInterface.EXTRA_TITLE);
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            //吐司自定义内容
            Log.e("lzx", "onReceive: "+title+"++++++"+message );
        }else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
//            Map map = (Map) bundle.getParcelable(JPushInterface.EXTRA_EXTRA);
//            String type = (String) map.get("type");
            String order=  bundle.getString(JPushInterface.EXTRA_EXTRA);
            String type=  bundle.getString(JPushInterface.EXTRA_ALERT);
            Map<String, Object> map = new HashMap<String, Object>();
            JSONObject jsonObject;
            try {
                jsonObject = new JSONObject(order);
                String orderId = jsonObject.getString("order");
                if (type !=null && type.contains("收款成功")){
                    EventBus.getDefault().post(new OrderIdEvent(orderId));
                }
//                Log.e("lzx", "onReceive: "+orderId );
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            EventBus.getDefault().post(new OrderListEvent());
        }else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())){
            String type=  bundle.getString(JPushInterface.EXTRA_EXTRA);
            Map<String, Object> map = new HashMap<String, Object>();
            JSONObject jsonObject;
            String type1 = null;
            try {
                jsonObject = new JSONObject(type);
                 type1 = jsonObject.getString("type");
                Log.e("lzx", "onReceive: "+type1 );
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                Intent i = new Intent(context, MessageActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
                EventBus.getDefault().post(new OrderListEvent());
                context.startActivity(i);
//            if ("apply".equals(type1)){
//
//                Intent i = new Intent(context, NewFriendActivity.class);
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//                context.startActivity(i);
//            }else if ("confirm".equals(type1)){
//                Intent i = new Intent(context, MyFriendActivity.class);
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//                context.startActivity(i);
//            }else if ("refused".equals(type1)){
//                Intent i = new Intent(context, FoundFriendActivity.class);
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//                context.startActivity(i);
//            }else if ("invite".equals(type1)){
//                Intent i = new Intent(context, InviteMessageActivity.class);
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//                context.startActivity(i);
//            }else if ("agree".equals(type1)){
//                Intent i = new Intent(context, InviteMessageActivity.class);
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//                context.startActivity(i);
//            }else if ("disagree".equals(type1)){
//                Intent i = new Intent(context, InviteMessageActivity.class);
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//                context.startActivity(i);
//            }else if ("addGroup".equals(type1)){
//                Intent i = new Intent(context, MyTeamActivity.class);
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//                i.putExtra("TAG","1");
//                context.startActivity(i);
//            }else if ("vice".equals(type1)){
//                Intent i = new Intent(context, MyTeamActivity.class);
//                i.putExtra("TAG","1");
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//                context.startActivity(i);
//            }else if ("transfer".equals(type1)){
//                Intent i = new Intent(context, MyTeamActivity.class);
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//                context.startActivity(i);
//            }else if ("kick".equals(type1)){
//                Intent i = new Intent(context, MyTeamActivity.class);
//                i.putExtra("TAG","1");
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//                context.startActivity(i);
//            }else if ("quit".equals(type1)){
//                Intent i = new Intent(context, MyTeamActivity.class);
//                i.putExtra("TAG","1");
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//                context.startActivity(i);
//            }
        }
    }
    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            }else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
                    Log.i("lzx", "This message has no Extra data");
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator<String> it =  json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next();
                        sb.append("\nkey:" + key + ", value: [" +
                                myKey + " - " +json.optString(myKey) + "]");
                    }
                } catch (JSONException e) {
                    Log.i("lzx", "Get message extra JSON error!");

                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }
}